using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Media;
using System;

namespace whereIsTheDonutGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Enemy : SuperPlayer
    {

        public bool alive, hit;
         private int hittime;
        SoundEffect soundEffect;
        SoundEffectInstance instance;
        Vector3 min, max;

        //donutGame game;
        public Enemy(donutGame game, int x, Vector3 startPosi)
            : base(game)
        {
            soundEffect = game.Content.Load<SoundEffect>("Sounds/gotHit");
            instance = soundEffect.CreateInstance();
            rotate(x);
            this.game = game;
            alive = true;
            position = startPosi;
            hittime = 0;
            hit = false;

        }

       
        public override void Update(GameTime gameTime)
        {
            KeyboardState state = Keyboard.GetState();
            movement = Vector3.Zero;
            movement.Z = -0.5f;
            

            Matrix facingMatrix = Matrix.CreateRotationY(rotation);
            Vector3 velocity = Vector3.Transform(movement, facingMatrix);
            velocity *= PlayerVelocity;

            Vector3 newPosition = position + velocity;
            bool collision = false;

            foreach (Solid s in game.cspawn.solids)
            {
                foreach (BoundingBox box in s.getBoundingBoxes())
                {
                    if (bbox.Intersects(box))
                        collision = true;
                }
            }
            if (!game.terrain.isValidPosition(newPosition) || game.terrain.Height[(int)newPosition.X, (int)-newPosition.Z] > game.terrain.Height[(int)position.X, (int)-position.Z] + 1 || collision)
            {
                rotate(180);
                facingMatrix = Matrix.CreateRotationY(rotation);
                velocity = Vector3.Transform(movement, facingMatrix);
                velocity *= PlayerVelocity;
                newPosition = position + velocity;
            }
            else
            {
                newPosition.Y = game.terrain.Height[(int)newPosition.X, (int)-newPosition.Z] + 0;
            }

            checkPlayerCollision(gameTime);
            checkBoxCollision();
            position = newPosition;
            UpdateBoundingBox();


            base.Update(gameTime);
        }

        private void checkBoxCollision()
        {
            foreach (MovableBox m in game.cspawn.boxes)
            {
                if (this.bbox.Intersects(m.boundingBox))
                {
                    newPosition = position;
                }
            }
        }

        private void checkPlayerCollision(GameTime gameTime)
        {
            if (hittime + 1 < gameTime.TotalGameTime.Seconds)
            {

                if (game.player.bbox.Intersects(this.bbox))
                {
                    if (game.player.Position.Y - 5  > position.Y)
                    {
                        alive = false;
                        game.Components.Remove(this);
                    }
                    else
                    {
                        game.player.life--;

                        hittime = gameTime.TotalGameTime.Seconds;
                        instance.Play();
                    }
                }
            } 
            else
            {

            }
        }

        public override void UpdateBoundingBox()
        {
            bbox = new BoundingBox(position - min, position + max);
        }

        protected override void LoadContent()
        {
            player = this.Game.Content.Load<Model>("enemy");
            List<Vector3> minmax = Stuff.CalculateBoundingBoxRange(player);
            min = minmax[0];
            max = minmax[1];
        }
    }
}